package com.zhou.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TestController
 *
 * @author zhouxianfeng
 * @date 2021-5-25
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RabbitTemplate template;

    @Autowired
    private Queue helloWorldQueue;

    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/commit")
    public void testTransactionCommit() {
        this.template.convertAndSend(helloWorldQueue.getName(), "testTransactionCommit");
        if (1 == 1) {
            throw new RuntimeException("test1");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/rollback")
    public void testTransactionRollback() {
        this.template.convertAndSend(helloWorldQueue.getName(), "testTransactionRollback");
        log.info("执行数据库成功");
    }
}
